package com.cloud.school.bus.teacherhelper.modules.stuclass.adapter;

import java.util.Calendar;
import java.util.List;

import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.DateUtils;
import com.android.support.jhf.utils.ImageUtil;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Student;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StudentAdapter extends BaseAdapter {

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private List<Student> mStudents;

	public StudentAdapter(Context context, List<Student> students) {
		mContext = context;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
		mStudents = students;
	}

	@Override
	public int getCount() {
		return mStudents.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mStudents.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_student, null);
		}

		Student student = mStudents.get(arg0);

		ImageView headImageView = (ImageView) arg1
				.findViewById(R.id.headImageView);
		if (!TextUtils.isEmpty(student.avatar)) {
			ImageLoader.getInstance().displayImage(student.avatar,
					headImageView, mApplication.mStudentCacheDisplayImageOptions,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							ImageView imageView = (ImageView) view;
//							imageView.setImageBitmap(loadedImage);
							if(imageUri.startsWith("http://")){
								imageView.setImageBitmap(loadedImage);
							}else if(imageUri.startsWith("file:///")){
								ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
							}else {
								ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
							}
						}
					});
		}
		TextView nameTextView = (TextView) arg1.findViewById(R.id.nameTextView);
		nameTextView.setText(student.enname);
		TextView ageTextView = (TextView) arg1.findViewById(R.id.ageTextView);
		ageTextView.setText(student.age + mContext.getString(R.string.age));
		TextView statusTextView = (TextView) arg1
				.findViewById(R.id.statusTextView);
		long orderendtime = 0L;
		boolean flag = false;
		try {
			orderendtime = Long.parseLong(student.orderendtime);
			orderendtime = orderendtime * 1000;
			Calendar endTimeCalendar = Calendar.getInstance();
			endTimeCalendar.setTimeInMillis(DateUtils.getDateMillis(
					DateUtils.dateFormat(orderendtime, "yyyy-MM-dd"),
					"yyyy-MM-dd"));
			Calendar currentTimeCalendar = Calendar.getInstance();
			currentTimeCalendar.setTimeInMillis(DateUtils.getDateMillis(
					DateUtils.dateFormat(System.currentTimeMillis(),
							"yyyy-MM-dd"), "yyyy-MM-dd"));
			if(currentTimeCalendar.compareTo(endTimeCalendar) < 0){
				flag = true;
			}
		} catch (Exception e) {
		}
		if (orderendtime > 0 && flag) {
//			statusTextView.setText("已开通");
			statusTextView.setText(mContext.getString(R.string.in_service));
			statusTextView.setTextColor(Color.BLACK);
		} else {
//			statusTextView.setText("尚未开通");
			statusTextView.setText(mContext.getString(R.string.not_yet_in_service));
			statusTextView.setTextColor(Color.RED);
		}

		return arg1;
	}

}
